package com.ag.hmk.action;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.ag.bean.TbCasekw;
import com.ag.bean.TbClassiccase;
import com.ag.common.IDUtil;
import com.ag.common.PageUtil;
import com.ag.hmk.service.impl.HmkUiltServImpl;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 
 * @author hmk
 * 查询案例列表(分页查询)
 */
public class FindSomeCaseAction extends ActionSupport {
	private static final long serialVersionUID = -4270657116058210604L;
	
	private HmkUiltServImpl hmkUiltServImpl;
	private int page;
	private int maxRow;
	private String typeID1;
	private String caselevel1;
	private String caseName1;
	private String caseDetail1;
	
	private PageUtil pageUtil; //返回结果
	private List<String> keywordList; //放回案例的关键字
	
	/*
	 * 查询案例列表(分页查询)
	 */
	public String findCaseList()throws Exception{
		List<String> strList = new ArrayList<String>();
		PageUtil pUtil = new PageUtil();
		List result=null;
		int total=0;
		try {
			String cql = "select count(*) from TbClassiccase c where 1=1";
			String hql = "from TbClassiccase c left join fetch c.tbType left join fetch c.tbLevel left join fetch c.tbCasekws ck where 1=1";
			System.out.println("----"+typeID1);
			if (typeID1!=null && typeID1.length()>0) {
				cql += " and c.tbType.tpyeId = " + typeID1;
				hql += " and c.tbType.tpyeId = " + typeID1;
			}
			System.out.println("----"+caselevel1);
			if (caselevel1!=null && caselevel1.length()>0) {
				cql += " and c.tbLevel.levelId = " + caselevel1;
				hql += " and c.tbLevel.levelId = " + caselevel1;
			}
			System.out.println("----"+caseName1);
			if (caseName1!=null && caseName1.length()>0) {
				cql += " and c.ccName like '%" + caseName1+"%'";
				hql += " and c.ccName like '%" + caseName1+"%'";
			}
			System.out.println("----"+caseDetail1);
			if (caseDetail1!=null && caseDetail1.length()>0) {
				hql += " and ck.CKw  like '%" + caseDetail1+"%'";
			}
			cql+=" and c.a='"+IDUtil.PRESERVE+"' ";
			hql+=" and c.a='"+IDUtil.PRESERVE+"' order by c.ccId desc";
			if (page == 0) {
				page = 1;
			}
			if (maxRow == 0) {
				maxRow = 10;
			}
			int firstRow = (page - 1) * maxRow;
			//分页查询
			result = hmkUiltServImpl.query(hql, firstRow, maxRow);
			if(result!=null){
				total = result.size();
				Iterator it1 = result.iterator();
				while(it1.hasNext()){
					String str1 = "";
					int i = 0;
					TbClassiccase tbcase = (TbClassiccase) it1.next();
					Set<TbCasekw> ckwSet = null ;
					ckwSet=( Set<TbCasekw>)tbcase.getTbCasekws();
					Iterator<TbCasekw> it2 =ckwSet.iterator();
					while(it2.hasNext()){
						str1 +=(it2.next().getCKw()+",");
						i++;
						if (i>4) {
							break;
						}
					}
					str1 +="...";
					strList.add(str1);
					}
			}
		} catch (Exception e) {
			e.printStackTrace();
			result=null;
			total=0;
			strList = null;
		}
		
		pUtil.setTotal(total);
		pUtil.setPage(page);
		pUtil.setMaxRow(maxRow);
		pUtil.setList(result);
		this.setKeywordList(strList);
		this.setPageUtil(pUtil);
		return "findCaseListOK";
	}

	public void setHmkUiltServImpl(HmkUiltServImpl hmkUiltServImpl) {
		this.hmkUiltServImpl = hmkUiltServImpl;
	}


	public PageUtil getPageUtil() {
		return pageUtil;
	}


	public void setPageUtil(PageUtil pageUtil) {
		this.pageUtil = pageUtil;
	}


	public void setPage(int page) {
		this.page = page;
	}


	public void setMaxRow(int maxRow) {
		this.maxRow = maxRow;
	}

	public void setTypeID1(String typeID1) {
		this.typeID1 = typeID1;
	}

	public void setCaselevel1(String caselevel1) {
		this.caselevel1 = caselevel1;
	}

	public void setCaseName1(String caseName1) {
		this.caseName1 = caseName1;
	}

	public void setCaseDetail1(String caseDetail1) {
		this.caseDetail1 = caseDetail1;
	}

	public List<String> getKeywordList() {
		return keywordList;
	}

	public void setKeywordList(List<String> keywordList) {
		this.keywordList = keywordList;
	}

	
	

}
